/*Copyright (C) 2009 - 2011 MASSART GAUTHIER

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../headers.h"

short ban_IA(short pot[13]) {
	short i = 0, choix = 0, pot_en_face = 0;

	CSLP_Sleep(1500);

	for(i = 8; i != 13; i++) {
		if(pot[i] + i == 14) {
			return i;
		}
	}

	for(i = 8; i != 13; i++) {
		if(pot[pot[i] + i] < 14 && pot[pot[i] + i] == 0 && pot[i] != 0) {
			switch(pot[i] + i) {
			case 8 :
				pot_en_face = 6;
				break;
			case 9 :
				pot_en_face = 5;
				break;
			case 10 :
				pot_en_face = 4;
				break;
			case 11 :
				pot_en_face = 3;
				break;
			case 12 :
				pot_en_face = 2;
				break;
			case 13 :
				pot_en_face = 1;
				break;
			default :
				break;
			}
			if(pot[pot_en_face] != 0) {
				return i;
			}
		}
	}

	for(i = 8; i != 13; i++) {
		if(pot[i] + i >= 14) {
			return i;
		}
	}

	while(choix == 0) {
		choix = random(8, 13);
		if(pot[choix] == 0) {
			choix = 0;
		}
	}

	return choix;
}
